<?php
	include_once "/home/mmediaai/www/ad/sys/com/model/util/PHPMailer/class.phpmailer.php";	
		
	
	header("Content-Type:text/html; charset=utf-8");
	/**
	 * Sending Email
	 * @param unknown_type $send
	 * @param unknown_type $subject
	 * @param unknown_type $content
	 */
	function email($sends, $subject, $content) {
		$mail = new PHPMailer();
		$mail->IsSMTP();                  // set mailer to use SMTP
		$mail->Host = "mail.mmedia.ai-ad.net.tw";  // specify main and backup server
		$mail->SMTPAuth = true;     // turn on SMTP authentication
		//$mail->SMTPSecure = "ssl";  //use ssl
		$mail->Username = "service@mmedia.ai-ad.net.tw";  // SMTP username
		$mail->Password = "service"; // SMTP password
		$mail->Port = 25;
		$mail->CharSet="utf-8";
		$mail->From = "service@mmedia.ai-ad.net.tw";
		$mail->FromName = "Amazing Mobile ADs";
		
		if (is_array($sends)) {
			foreach($sends as $send) {
				$mail->AddAddress($send);
			}
		} else {
			$mail->AddAddress($sends);
		}
		//$mail->AddAddress("ellen@example.com");                  // name is optional
		//$mail->AddReplyTo("info@example.com", "Information");
		//$mail->WordWrap = 50;                                 
		//$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
		//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
		$mail->IsHTML(true);                                  // set email format to HTML
		$mail->Subject = $subject;
		$mail->Body = $content;
    	return $mail->Send();
	}
	
	 
	 
	include_once '/home/mmediaai/www//ad/sys/com/model/db/DBConn.php';	
	
	$conn = new DBConn ();
	
	
	$sql = "select * from  user where  
			count <500  and Status =1 and datediff(curdate(), NotifyTime) > 7 and (type =1 or type =2)
			and (NotifyTime is null or datediff(curdate(), NotifyTime) > 30)";
	
	 $result = mysql_query($sql);
	
	 $row_set = array ();

 
	date_default_timezone_set('Asia/Taipei');
	 
	$nowDate = date("Y-m-d H:i:s");
	 
	 while ($row = @mysql_fetch_object ( $result ) ) {
	 	if ($row->Count ==0 ) { //無點數通知
	 		email("$row->Account", 
	 			  "Amazing Mobile ADs 通知:您帳號餘額已不足",
	 			  "親愛的   $row->Name  您好， <BR><BR>

				        感謝您購買Amazing Mobile Ads廣告， <BR>
				        提醒您，您目前帳戶的餘額已剩0元，無法投播任何廣告，<BR>
				        如果仍有廣告刊登需求，請您立即通知我們客服人員進行加值。 <BR>
				        請注意，我們只會根據您的廣告所收到的展示次數或點擊量收取廣告費用。廣告刊登總金額將不會超過您所設定的每日行銷預算。<BR> 
				        如果您對於廣告上有任何問題，請來信至: service@ai-ad.net.tw <BR>
				        謝謝您， <BR>
				   Amazing Mobile Ads敬上
	 			  "
	 		);	
	 		$sql = "update  user set  NotifyTime='$nowDate' where account='$row->Account'";	
		 	mysql_query($sql);
	 	} else if ($row->Count <500) { //小於五百點 通知
	 		email("$row->Account", 
	 			  "Amazing Mobile ADs　通知:您可能需要儲值了",
	 			  "親愛的   $row->Name   您好，  <BR> <BR>

				       感謝您購買Amazing Mobile Ads廣告，  <BR>
				       提醒您，您目前帳戶的餘額已不足500元， <BR>
				       如果仍有廣告刊登需求，請您立即通知我們客服人員進行加值。  <BR>
				       請注意，我們只會根據您的廣告所收到的展示次數或點擊量收取廣告費用。廣告刊登總金額將不會超過您所設定的每日行銷預算。 <BR> 
				       如果您對於廣告上有任何問題，請來信至: service@ai-ad.net.tw  <BR>
				       謝謝您，  <BR>
				   Amazing Mobile Ads敬上"
	 		);
	 		$sql = "update  user set  NotifyTime='$nowDate' where account='$row->Account'";	
	 		mysql_query($sql);
	 	}
	 	//$sql = "update  user set  NotifyTime='$nowDate' where account='$row->Account'";		 
	 	//mysql_query($sql);
	 }
		 
	 @mysql_free_result ( $result );
	 @mysql_close();
	 


	
?>